package com.vuespringboot.demo.controller;

import com.vuespringboot.demo.entity.*;
import com.vuespringboot.demo.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping ("/config")
public class ConfigController {

	@Autowired
	private InstrumentRepository instrumentRepository;
	@Autowired
	private PromptRepository promptRepository;
	@Autowired
	private EquipmentRepository equipmentRepository;
	@Autowired
	private OperationRepository operationRepository;
	@Autowired
	private SampleRepository sampleRepository;
	@Autowired
	private SpreadRepository spreadRepository;

	//分页查询
	@GetMapping ("/queryall/{page}/{size}")
	public Page<Instrument> queryAll(@PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable = PageRequest.of(page - 1, size);
		return instrumentRepository.findAll(pageable);
	}

	@GetMapping ("/opqueryall/{page}/{size}")
	public Page<Operation> opqueryAll(@PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable2 = PageRequest.of(page - 1, size);
		return operationRepository.findAll(pageable2);
	}

	@GetMapping ("/prqueryall/{page}/{size}")
	public Page<Prompt> prqueryAll(@PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable3 = PageRequest.of(page - 1, size);
		return promptRepository.findAll(pageable3);
	}

	@GetMapping ("/saqueryall/{page}/{size}")
	public Page<Sample> saqueryAll(@PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable4 = PageRequest.of(page - 1, size);
		return sampleRepository.findAll(pageable4);
	}

	@GetMapping ("/eqqueryall/{page}/{size}")
	public Page<Equipment> eqqueryAll(@PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable5 = PageRequest.of(page - 1, size);
		return equipmentRepository.findAll(pageable5);
	}

	@GetMapping ("/spqueryall/{page}/{size}")
	public Page<Spread> spqueryAll(@PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable6 = PageRequest.of(page - 1, size);
		return spreadRepository.findAll(pageable6);
	}

	//根据名称模糊分页查询
	@GetMapping ("/inbynamequeryall/{page}/{size}")
	public Page<Instrument> findByinNameContainingOrderByInIdDesc(String name, @PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable7 = PageRequest.of(page - 1, size);
		return instrumentRepository.findByinNameContainingOrderByInIdDesc(name, pageable7);
	}

	//根据spChoose分页查询
	@GetMapping ("findBySpChoose/{page}/{size}")
	public Page<Spread> findBySpChoose(int spChoose, @PathVariable ("page") Integer page, @PathVariable ("size") Integer size) {
		Pageable pageable8 = PageRequest.of(page - 1, size);
		return spreadRepository.findBySpChoose(spChoose, pageable8);
	}


	//根据名称模糊查询
	@GetMapping ("/findByNameLike")
	public List<Instrument> findByNameLike(String inName) {
		return instrumentRepository.findByNameLike(inName);
	}


	//查询所有
	@GetMapping ("/queryall/list")
	public List<Instrument> querAll() {
		return instrumentRepository.findAll();
	}
	@GetMapping ("/speryall/list")
	public List<Spread> spquerAll() {
		return spreadRepository.findAll();
	}
	@GetMapping ("/preryall/list")
	public List<Prompt> prquerAll() {
		return promptRepository.findAll();
	}
	@GetMapping ("/saeryall/list")
	public List<Sample> saquerAll() {
		return sampleRepository.findAll();
	}


	//添加
	@PostMapping ("/save")
	public String addStu(@RequestBody Instrument instrument) {
		Instrument instrument1 = instrumentRepository.save(instrument);
		if (instrument1 != null) {
			return "true";
		}
		return "false";
	}

	//修改
	@PutMapping ("/upd")
	public String upd(@RequestBody Instrument instrument) {
		Instrument instrument1 = instrumentRepository.save(instrument);
		if (instrument1 != null) {
			return "true";
		}
		return "false";
	}

	@PutMapping ("/opupd")
	public String opupd(@RequestBody Operation operation) {
		Operation operation1 = operationRepository.save(operation);
		if (operation1 != null) {
			return "true";
		}
		return "false";
	}

	@PutMapping ("/prupd")
	public String prupd(@RequestBody Prompt prompt) {
		Prompt prompt1 = promptRepository.save(prompt);
		if (prompt1 != null) {
			return "true";
		}
		return "false";
	}

	@PutMapping ("/saupd")
	public String saupd(@RequestBody Sample sample) {
		Sample sample1 = sampleRepository.save(sample);
		if (sample1 != null) {
			return "true";
		}
		return "false";
	}

	@PutMapping ("/equpd")
	public String equpd(@RequestBody Equipment equipment) {
		Equipment equipment1 = equipmentRepository.save(equipment);
		if (equipment1 != null) {
			return "true";
		}
		return "false";
	}

	@PutMapping ("/spupd")
	public String spupd(@RequestBody Spread spread) {
		Spread spread1 = spreadRepository.save(spread);
		if (spread1 != null) {
			return "true";
		}
		return "false";
	}


	//删除
	@DeleteMapping ("/deleteById/{id}")
	public void deleteById(@PathVariable ("id") Integer id) {
		instrumentRepository.deleteById(id);
	}

}
